<template>
  <div class="menu-demo">
    <a-row
      id="globalHeader"
      class="grid-demo"
      style="margin-bottom: 16px"
      align="center"
      :wrap="false"
    >
      <a-col flex="auto">
        <a-menu
          mode="horizontal"
          :selected-keys="selectedKeys"
          :default-selected-keys="['1']"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img class="logo" src="../assets/logo_joj.png" />
              <div class="title">JOJ-智能阅卷系统</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoutes" :key="item.path"
            >{{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="100px">
        <div class="">
          {{ store.state.user.loginUser.userName }}
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import accessEnum from "@/access/accessEnum";

//userRouter相当于$router,它是VueRouter的一个实例。这个实例对象包含了整个应用的路由信息
const router = useRouter();
//默认路由，路由高亮显示
const selectedKeys = ref(["/"]);
//路由
const store = useStore();
//当前登录用户
const loginUser = store.state.user.loginUser;

//路由跳转时，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     username: "蒋宇博",
//     userRole: accessEnum.USER,
//   });
//   //console.log(store.state.user.loginUser);
// }, 3000);

// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    //是否是需要隐藏的菜单（比如没有权限）
    if (item.meta?.hide) {
      return false;
    }
    // 根据权限过滤菜单
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});
</script>
<style scoped>
.menu-demo {
  box-sizing: border-box;
  width: 100%;
  padding: 0px;
  position: center;
}

.title-bar {
  display: flex;
  align-items: center;
}

.title {
  margin-left: 15px;
  color: #444;
  margin-right: 15px;
}

.logo {
  height: 56px;
}
</style>
